<template>
  <div class="app-container">
    <div class="app-title">
      <span>无指标挂接指标单位</span>
    </div>
    <!-- 按钮 -->
    <div class="filter-container">
      <div>
        <el-col style="text-align:right;margin-top: -8px;">
           <el-button
            size="small"
            class="filter-item"
            style="margin-left: 10px;"
            type="primary"
            @click="moreQuery"
          >
            查询
          </el-button>
          <el-button
            size="small"
            class="filter-item"
            style="margin-left: 10px;"
            type="primary"
            @click="handleAdd"
          >
            新增
          </el-button>
          <el-button
            size="small"
            class="filter-item"
            style="margin-left: 10px;"
            type="primary"
            @click="handleEdit"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            class="filter-item"
            type="primary"
            :loading="deleteLoading"
            @click="handleDelete"
          >
            删除
          </el-button>
        </el-col>
      </div>
    </div>
<el-dialog
          v-dialogDrag
          title="查询"
          :visible.sync="dialogQueryVisible"
          :close-on-click-modal="false"
          width="50%"
          style="padding:0px"
        >
          <el-form :model="listQuery" label-position="right" label-width="100px" class="dialog-backgroundcolor" size="small">
            <el-row>
              <el-col :span="12">
                <el-form-item label="单位名称：">
                  <el-input v-model="listQuery.agencyName" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单位编码：">
                  <el-input v-model="listQuery.agencyCode" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="指标单位名称">
                  <el-input v-model="listQuery.hookupAgencyName" />
                </el-form-item>
              </el-col>
               <el-col :span="12">
                <el-form-item label="指标单位编码：">
                  <el-input v-model="listQuery.hookupAgencyCode" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col style="text-align: right; margin-top: 1%">
                <el-button size="small" class="filter-item" style="margin-left: 10px;" type="primary" @click="handleFilter">
                  查询
                </el-button>
              </el-col>
            </el-row>
          </el-form>
        </el-dialog>

    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :height="tableHeight"
      :data="list"
      :stripe="true"
      fit
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column type="index" align="center" width="50" label="序号" />
      <el-table-column label="单位名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.agencyName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位编码" align="center">
        <template slot-scope="{row}">
          <span>{{ row.agencyCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="指标单位名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.hookupAgencyName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="指标单位编码" align="center">
        <template slot-scope="{row}">
          <span>{{ row.hookupAgencyCode }}</span>
        </template>
      </el-table-column>

    </el-table>
    <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <!-- 新增/编辑页面部分 -->
    <el-dialog  v-dialogDrag  
      :close-on-click-modal="false"
      :title="dialogStatus"
      :visible.sync="dialogFormVisible"
      width="20%"
    >
      <el-form
        ref="dataForm"
        class="dialog-backgroundcolor"
        :rules="dataFormRules"
        :model="formData"
        label-position="top"
        label-width="120px"
        size="small"
      >
        <div class="formDeviation">
          <!-- <el-row>
            <el-col>
              <el-form-item
                label="单位名称："
                prop="showName"
                class="item-margin"
              >
                <el-input
                  v-model="formData.showName"
                  maxlength="200"
                /></el-form-item>
            </el-col>
          </el-row> -->
          <el-row>
            <el-col>
              <el-form-item
                label="单位编码："
                prop="agencyCode"
                class="item-margin"
              >
                <el-input
                  v-model="formData.agencyCode"
                  maxlength="200"
                /></el-form-item>
            </el-col>
          </el-row>
          <!-- <el-row>
            <el-col>
              <el-form-item
                style="margin-top:10px"
                label="指标单位名称："
                prop="payeeAcctNo"
                class="item-margin"
              >
                <el-input
                  v-model="formData.payeeAcctNo"
                  maxlength="200"
                /></el-form-item>
            </el-col>
          </el-row> -->
          <el-row>
            <el-col>
              <el-form-item
                style="margin-top:10px"
                label="指标单位编码："
                prop="hookupAgencyCode"
                class="item-margin"
              >
                <el-input
                  v-model="formData.hookupAgencyCode"
                  maxlength="30"
                /></el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-row>
          <el-col
            style="text-align: center; margin-top:20px; margin-bottom:8px"
          >
            <el-button
              type="primary"
              :loading="saveLoading"
              @click="updateData()"
            >
              保存
            </el-button>
            <el-button @click="dialogFormVisible = false">
              取消
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {getSalaryPaymentAgency,updateHookupAgency,saveHookupAgency,deleteHookupAgency} from '@/api/salaryConfigManage/salaryPaymentAgency'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'
export default {
  name: 'Unit',
  components: { Pagination, tableEmpty },
  filters: {
  },
  data() {
    return {
      tableHeight: null,
      that: this,
      list: null,
      total: 0,
      listLoading: true,
      saveLoading: false,
      deleteLoading: false,
      dialogImportFormVisible: false,
      isEdit: false,
      listQuery: {
        page: 1,
        limit: 200
      },
      formData: {
        id: '',
        mofDivCode: '', // 区划
        agencyName: '',//单位名称
        agencyCode: '', // 单位编码
        hookupAgencyName: '', // 挂接单位名称
        hookupAgencyCode: '',//挂接单位编码
     
      },
      dialogFormVisible: false,
      dialogStatus: '',
      dataFormRules: {
        agencyCode: [
          { required: true, message: '请输单位编码', trigger: 'blur' }
        ],
        hookupAgencyCode: [
          { required: true, message: '请输入指标单位编码', trigger: 'blur' },
        ],
      },
      dialogQueryVisible: false
    }
  },
  watch: {
    // 表格的数据数组
    list(val) {
      this.doLayout()
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },
  methods: {
    /* 重新渲染table组件 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },
    // 获取table高度的方法
    getTbHeight() {
      this.tableHeight = window.innerHeight - 198
    },
    getList() {
      this.listLoading = true
      getSalaryPaymentAgency(this.listQuery).then((response) => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    }, 
    moreQuery() {
      this.listQuery = Object.assign({}, this.listQuery, '')
      this.dialogQueryVisible = true
    },  
    handleFilter() {
       this.dialogQueryVisible = false
      this.listQuery.page = 1
      this.getList()
    },
    // 新增
    handleAdd() {
      this.formData = Object.assign({}, '')
      this.formData.id = ''
      this.dialogStatus = '新增'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 编辑
    handleEdit() {
      const selection = this.$refs.singalTable.selection
      if (selection.length !== 1) {
        this.$message({
          showClose: true,
          message: '请选择要编辑的收款账户',
          type: 'warning',
          duration: 4000
        })
        return
      }
      this.dialogStatus = '编辑'
      this.dialogFormVisible = true
      this.formData = Object.assign({}, this.formData, selection[0])
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 保存
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          const tempData = Object.assign({}, this.formData)
          // const param = { 'payeeAcctNo': this.formData.payeeAcctNo,
          //   'payeeAcctCode': this.formData.payeeAcctCode,
          //   'id': this.formData.id }
              if (tempData.id === '') { // 新增
              //saveHookupAgency
                saveHookupAgency(tempData).then((response) => {
                  this.dialogFormVisible = false
                  this.$message({
                    showClose: true,
                    message: '保存成功',
                    type: 'success',
                    duration: 4000
                  })
                  this.saveLoading = false
                  this.handleFilter()
                }).catch(() => {
                  this.saveLoading = false
                })
              } else { // 修改
                updateHookupAgency(tempData).then((response) => {
                  this.dialogFormVisible = false
                  this.$message({
                    showClose: true,
                    message: '编辑成功',
                    type: 'success',
                    duration: 4000
                  })
                  this.saveLoading = false
                  this.handleFilter()
                }).catch(() => {
                  this.saveLoading = false
                })
              }
        }
      })
    },
    // 删除
    handleDelete() {
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要删除的数据',
          type: 'warning',
          duration: 4000
        })
        return
      }
      this.deleteLoading = true
      const operate = []
      selection.forEach(item => {
        operate.push(item.id)
      })
      this.$confirm('确定删除吗?', '提示', {
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteHookupAgency(operate).then((response) => {
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success',
            duration: 4000
          })
          this.handleFilter()
          this.deleteLoading = false
        }).catch(() => {
          this.deleteLoading = false
        })
      }).catch(() => {
        this.deleteLoading = false
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.formDeviation {
  margin-left: 10%;
  .el-form-item__error {
    padding-top: 5%;
  }
}
.redstar.el-form-item__error{
  margin-top: 35px !important;
  left: -96px !important;
}
</style>
